define(['store'], function (store) {
    'use strict';
    function ctrl($scope, $ionicLoading, $timeout, $state, LocalCacheService, CommonService, WTService,IdentificationService) {
        var timer;
        var seconds;
        var par = $scope.param = {};

        function updateSeconds() {
            if (seconds == 0) {
                $scope.buttonState.checkState = false;
                $scope.buttonState.checkText = '获取验证码';
                seconds = 180;
                return;
            }
            seconds--;
            $scope.buttonState.checkText = seconds + '秒后失效';
            timer = $timeout(function () {
                updateSeconds();
            }, 1000);
        }

        function init() {
            if(timer){
                $timeout.cancel(timer);
            }
            seconds = 180;
            $ionicLoading.show();
            IdentificationService.initCheck()
                .then(function (data) {
                    if (data.error_no != 0) {
                        $ionicLoading.hide();
                        CommonService.showAlert({message: data.error_info});
                        return;
                    }
                    var hasUploadIDCard=data.hasUploadIDCard;
                   IdentificationService.getCheckRecordStatus()
                        .then(function (data) {
                            $ionicLoading.hide();
                            if (data.error_no != 0) {
                                CommonService.showAlert({message: data.error_info});
                                return;
                            }
/*                           if (data.idcard_check_record_status == 3) {
                                $scope.status = true;
                                $scope.subtext = '您已提交手机号重置申请，系统正在处理中。如有疑问，请拨打客服热线：400-800-5000';
                                return;
                            }*/
                            //获取当前手机号重置状态
                            $scope.status = false;
                            //渠道统计相关
                            par.source = CommonService.getSource();
                            $scope.mobileNew = {
                                mobile: '',
                                vCode: ''
                            };
                            $scope.buttonState = {
                                checkState: false,
                                submitState: false,
                                checkText: '获取验证码'
                            };
                            $scope.formEvents = {
                                getCode: function () {
                                    par.recommendInfos = LocalCacheService.getRecommendInfos();
                                    var params2 = {mobile_tel: $scope.mobileNew.mobile,type:'2',app_id:'gcszt'};
                                    $scope.buttonState.checkState = true;
                                    WTService.sendMobileCode(params2).then(
                                        function () {
                                            updateSeconds();
                                        },
                                        function (result) {
                                            $scope.buttonState.checkState = false;
                                            var message=CommonService.getErrorInfo(result);
                                            if (message=='参数传入错误'){
                                                message='请输入正确的手机号码';
                                            }
                                            CommonService.showAlert({message: message});
                                        }
                                    );

                                },
                                next: function () {
                                    // TODO 调用手机验证接口
                                    $ionicLoading.show();
                                    //验证手机验证码
                                    var params1 = {mobile_tel: $scope.mobileNew.mobile, mobile_code: $scope.mobileNew.vCode,app_id:'gcszt'};
                                    WTService.checkMobileCode(params1).then(
                                        function (data) {
                                            $ionicLoading.hide();
                                            if (data.error_info) {
                                                CommonService.showAlert({message: data.error_info});
                                            } else {
                                                store.set('idCardCheck', {mobile_tel: $scope.mobileNew.mobile});
                                                if (hasUploadIDCard == 'true') {
                                                    var iDate = {
                                                        business_info: $scope.mobileNew.mobile,
                                                        status: '2'
                                                    };
                                                    IdentificationService.uploadCheckSumbit(iDate)
                                                        .then(function (data) {
                                                                $ionicLoading.hide();
                                                                if (data.error_no != 0) {
                                                                    CommonService.showAlert({message: data.error_info});
                                                                    return;
                                                                }
                                                                $state.go('tab.account-resetPhoneThird');
                                                            }, function (result) {
                                                                $ionicLoading.hide();
                                                                CommonService.showAlert({message: result.error_info});
                                                            }
                                                        );
                                                } else {
                                                    $state.go('tab.account-resetPhoneSecond');
                                                }
                                            }
                                        },
                                        function (result) {
                                            $ionicLoading.hide();
                                            var message=CommonService.getErrorInfo(result);
                                            switch (message){
                                                case '参数传入错误' : message='请输入正确的手机号码及验证码';
                                                    break;
                                                case '未知错误' : message='验证码错误';
                                                    break;
                                            }
                                            CommonService.showAlert({message: message});
                                            store.set('idCardCheck', {mobile_tel: $scope.mobileNew.mobile});
                                            $state.go('tab.account-resetPhoneSecond');
                                        }
                                    );
                                }
                            }

                        }, function (result) {
                            $ionicLoading.hide();
                            CommonService.showAlert({message: result.error_info});
                        });
                }, function (result) {
                    $ionicLoading.hide();
                    CommonService.showAlert({message: result.error_info});
                    $state.go('tab.account-manager');
                })
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });
    }

    ctrl.$inject = ['$scope', '$ionicLoading', '$timeout', '$state', 'LocalCacheService', 'CommonService', 'WTService', 'IdentificationService'];
    return ctrl;
});